package com.forum.service.impl;

import com.forum.context.BaseContext;
import com.forum.dto.FeedBackReplyDTO;
import com.forum.dto.FeedbackDTO;
import com.forum.dto.FeedbackPageQueryDTO;
import com.forum.entity.Feedback;
import com.forum.mapper.FeedbackMapper;
import com.forum.result.PageResult;
import com.forum.service.FeedbackService;
import com.forum.utils.IpAddressUtil;
import com.forum.vo.FeedbackStatusVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

import static com.forum.constant.FeedbackStatusConstant.*;

@Service
@Slf4j
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 用户提交反馈信息
     */
    @Override
    public void save(FeedbackDTO feedbackDTO, HttpServletRequest request) {
        Feedback feedback = new Feedback();
        Long userId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(feedbackDTO, feedback);
        feedback.setUserId(userId);
        feedback.setStatus(FEEDBACK_STATUS_PENDING);
        // 获得登录人的ip地址防刷
        String clientIp = IpAddressUtil.getClientIpAddress(request);
        log.info("登录用户IP地址：" + clientIp);
        feedback.setIpAddress(clientIp);
        feedbackMapper.save(feedback);

    }

    /**
     * 管理员查询反馈信息
     * @return
     */
    @Override
    public PageResult list(FeedbackPageQueryDTO feedbackPageQueryDTO) {
        PageHelper.startPage(feedbackPageQueryDTO.getPage(), feedbackPageQueryDTO.getPageSize());
        Page<Feedback> page = feedbackMapper.queryList(feedbackPageQueryDTO);
        long total = page.getTotal();
        List<Feedback> result = page.getResult();
        return new PageResult(total, result);
    }

    @Override
    public void reply(FeedBackReplyDTO feedBackReplyDTO) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedBackReplyDTO, feedback);
        if (feedBackReplyDTO.getReplyContent() != null){
            LocalDateTime replyTime = LocalDateTime.now();
            feedback.setReplyTime(replyTime);
            Long HandlerId = BaseContext.getCurrentId();
            feedback.setHandlerId(HandlerId);
            feedbackMapper.reply(feedback);
        }else {
            feedback.setStatus(FEEDBACK_STATUS_PROCESSING);
            feedbackMapper.updateStatus(feedback);
        }


    }

    @Override
    public FeedbackStatusVO statistics() {
        Integer pending =feedbackMapper.statistics(FEEDBACK_STATUS_PENDING);
        Integer processing = feedbackMapper.statistics(FEEDBACK_STATUS_PROCESSING);
        Integer solved = feedbackMapper.statistics(FEEDBACK_STATUS_SOLVED);

        return FeedbackStatusVO.builder()
                .pendingNumber(pending)
                .beingProcessed(processing)
                .completedNumber(solved).build();
    }
}
